package com.tg.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.tg.app.R;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.AlertSoundHelper;
import com.demo.view.RecordAudioView;
import com.demo.view.WaveView;
import com.tange.base.toolkit.FileUtil;
import com.tg.appcommon.android.MediaPlayerHelper;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tg.data.helper.SoundHelper;
import com.tg.data.http.entity.DeviceBellBean;


import java.io.File;

public class AlertPersonalSoundItemView extends ConstraintLayout {
    private RecordAudioView btnSpeaking;
    private WaveView waveView;
    private Button btnSave;
    private Button btnPlay;
    private boolean isCustomSave = false;
    private TextView recorderTimeText;
    private String from;
    public boolean isCustomSave() {
        return isCustomSave;
    }
    public Button getBtnSave() {
        return btnSave;
    }
    private final MediaPlayerHelper mediaPlayerHelper = new MediaPlayerHelper();
    private SoundHelper soundHelper;
    public void setCustomSave(boolean customSave) {
        isCustomSave = customSave;
    }

    public void setFrom(String from){
        this.from = from;

    }
    public AlertPersonalSoundItemView(Context context) {
        super(context);
        initView(context);
    }

    public AlertPersonalSoundItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AlertPersonalSoundItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TextView getRecorderTimeText() {
        return recorderTimeText;
    }

    public WaveView getWaveView() {
        return waveView;
    }


    private void initView(Context context) {
        soundHelper = AlertSoundHelper.getInstance();
        View view = LayoutInflater.from(context).inflate(R.layout.activity_custom_alert_sound_personal, this);
        btnSpeaking = view.findViewById(R.id.btn_alert_sound_custom_speaking);
        btnSpeaking.setLongClickRecord(true);
        waveView = view.findViewById(R.id.camera_live_waveview);
        recorderTimeText = view.findViewById(R.id.tv_alert_sound_custom_time);
        btnPlay = view.findViewById(R.id.btn_alert_sound_custom_play);
        btnSave = view.findViewById(R.id.btn_alert_sound_custom_save);
        setButtonEnable(false);

        setListener();
    }

    private File getTempFile(){
        return AlertSoundHelper.getInstance().getTempFile(TGApplicationBase.getContext());
    }

    private void setListener() {
        btnPlay.setOnClickListener(v -> {
            File file = getTempFile();
            if (file.exists()) {
                playSound(file.getAbsolutePath());
            }
        });
    }

    private String getNextPersonalFileDescription(){
        return soundHelper.getNextPersonalFileDescription(ResourcesUtil.getString(R.string.txt_personal_recording));
    }

    public boolean save() {
        File tempFile = getTempFile();
        byte[] data = FileUtil.readFile(tempFile);
        if (data != null && data.length > 0) {
            String description = getNextPersonalFileDescription();
            if (StringUtils.isEmpty(description)){
                return false;
            }
            long time = System.currentTimeMillis();
            TGLog.d("description = " + description);
            String name = time + DeviceBellBean.PERSONAL_FILE_FLAG + description;
            String path = soundHelper.getDirectory() + File.separator + name + AlertSoundHelper.FORMAT_WAV_SUFFIX;
            File file = new File(path);
            if (file.exists()) {
                FileUtil.delete(file.getAbsolutePath());
            }
            FileUtil.wirteFile(file, data);
            setCustomSave(true);
            soundHelper.addFile(name);

            return true;
        }
        return false;
    }

    public void setButtonEnable(boolean enabled){
        ActivityHelper.setButtonEnable(btnPlay, enabled);
        ActivityHelper.setButtonEnable(btnSave, enabled);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        releaseMediaPlayer();
    }

    public void addWaveData(short data){
        waveView.addData(data);
    }

    public void setRecordAudioListener(RecordAudioView.IRecordAudioListener recordAudioListener){
        btnSpeaking.setRecordAudioListener(recordAudioListener);
    }

    private void playSound(String play) {
        mediaPlayerHelper.playSound(play);
    }

    public void releaseMediaPlayer() {
        mediaPlayerHelper.releaseMediaPlayer();
    }
}
